package com.ruoyi.project.pro.picture.controller;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pro.classes.domain.ProClasses;
import com.ruoyi.project.pro.classes.domain.ProClassesVo;
import com.ruoyi.project.pro.classes.service.IClassesService;
import com.ruoyi.project.pro.line.domain.ProLine;
import com.ruoyi.project.pro.line.domain.ProLineVo;
import com.ruoyi.project.pro.line.service.ILineService;
import com.ruoyi.project.pro.manager.domain.ProManager;
import com.ruoyi.project.pro.manager.domain.ProManagerVo;
import com.ruoyi.project.pro.manager.service.IProManagerService;
import com.ruoyi.project.pro.picture.domain.ProPicture;
import com.ruoyi.project.pro.picture.service.IProPictureService;
import com.ruoyi.project.pro.size.domain.ProSize;
import com.ruoyi.project.pro.size.domain.ProSizeVo;
import com.ruoyi.project.pro.size.service.ISizeService;
import com.ruoyi.project.pro.style.domain.ProStyle;
import com.ruoyi.project.pro.style.domain.ProStyleVo;
import com.ruoyi.project.pro.style.service.IStyleService;
import com.ruoyi.project.pro.texture.domain.ProTexture;
import com.ruoyi.project.pro.texture.domain.ProTextureVo;
import com.ruoyi.project.pro.texture.service.ITextureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-19 09:49
 * @description: 产品图库的控制类
 **/
@Controller
@RequestMapping("/pro/picture")
public class ProPictureController extends BaseController {
    @Autowired
    private IProPictureService proPictureService;

    @Autowired
    private IProManagerService proManagerService;

    @Autowired
    private ILineService lineService;
    @Autowired
    private ISizeService sizeService;
    @Autowired
    private IStyleService styleService;


    @Autowired
    private ITextureService textureService;

    @Autowired
    private IClassesService classesService;

    private String prefix = "pro/picture";

    /**
     * 跳转到图库的html页面,在跳转之前,所有的图片
     *
     * @return 产品图片的页面地址
     */
    @RequiresPermissions("pro:picture:view")
    @GetMapping("/picture")
    public String picture(ModelMap modelMap, String managerName) {
        List<ProPicture> proPictures = proPictureService.selectPicture(managerName);
        modelMap.addAttribute("pictures", proPictures);
        return prefix + "/picture";
    }
    /*allPicture*/

    @RequiresPermissions("pro:picture:view")
    @GetMapping("/allPicture")
    public String allPicture(ModelMap modelMap) {
        List<ProPicture> proPictures = proPictureService.selectAllPicture();
        System.out.println("allPicture"+proPictures);
        modelMap.addAttribute("pictures", proPictures);
        return prefix + "/allPicture";
    }

    /**
     * 将pictureId传给添加的页面
     */
    @PostMapping("/addShow")
    public String addShow(ModelMap modelMap,String pictureId){
        selectProInfo(modelMap, pictureId);
        return "pro/manager/add";
    }


    private void selectProInfo(ModelMap modelMap, String pictureId) {
        modelMap.addAttribute("pictureId",pictureId);
        //通过pictureId查找图片的名字,并放到modelMap中

        ProPicture proPicture = proPictureService.selectPictureById(Long.valueOf(pictureId));
        String pictureName = proPicture.getPictureName();
        modelMap.addAttribute("pictureName",pictureName);

        ProLine proLine=new ProLine();
        ProSize proSize=new ProSize();
        ProStyle proStyle=new ProStyle();
        ProTexture proTexture=new ProTexture();
        ProClasses proClasses=new ProClasses();
        List<ProClasses> proClassess = classesService.selectClasses(proClasses);
        List<ProSize> proSizes = sizeService.selectSize(proSize);
        List<ProLine> proLines = lineService.selectLine(proLine);
        List<ProStyle> proStyles = styleService.selectStyle(proStyle);
        List<ProTexture> proTextures = textureService.selectTexture(proTexture);
        List<ProStyleVo> proStyleVos=new ArrayList<>();
        List<ProLineVo> proLineVos=new ArrayList<>();
        List<ProSizeVo> proSizeVos=new ArrayList<>();
        List<ProClassesVo> proClassesVos=new ArrayList<>();
        List<ProTextureVo> proTextureVos=new ArrayList<>();
        for (ProTexture texture : proTextures) {
            ProTextureVo proTextureVo=new ProTextureVo();
            BeanUtils.copyBeanProp(proTextureVo,texture);
            proTextureVos.add(proTextureVo);

        }
        for (ProClasses proClasse : proClassess) {
            ProClassesVo proClassesVo=new ProClassesVo();
            BeanUtils.copyBeanProp(proClassesVo,proClasse);
            proClassesVos.add(proClassesVo);

        }
        for (ProStyle style : proStyles) {
            ProStyleVo proStyleVo=new ProStyleVo();
            BeanUtils.copyBeanProp(proStyleVo,style);
            proStyleVos.add(proStyleVo);

        }
        for (ProSize size : proSizes) {
            ProSizeVo proSizeVo=new ProSizeVo();
            BeanUtils.copyBeanProp(proSizeVo,size);
            proSizeVos.add(proSizeVo);

        }
        for (ProLine line : proLines) {
            ProLineVo proLineVo=new ProLineVo();
            BeanUtils.copyBeanProp(proLineVo,line);
            proLineVos.add(proLineVo);
        }
        modelMap.addAttribute("proLineVos",proLineVos);
        modelMap.addAttribute("proSizeVos",proSizeVos);
        modelMap.addAttribute("proStyleVos",proStyleVos);
        modelMap.addAttribute("proTextureVos",proTextureVos);
        modelMap.addAttribute("proClassesVos",proClassesVos);
    }


    /**
     * 通过id将产品表中的状态修改成1
     *
     * @param pictureId
     * @return
     */
    @RequiresPermissions("pro:picture:addPerFav")
    @PostMapping("/addPerFav")
//    @ResponseBody
    public String addPerFav(Long pictureId) {

        proManagerService.updateStatusById(pictureId);
        return "pro/manager/PerFav";
//        return toAjax(proManagerService.updateStatusById(pictureId));
    }

    /**
     * 通过id将产品表中的状态修改成1
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("pro:picture:addPerFav")
    @PostMapping("/removeFav")
    @ResponseBody
    public AjaxResult removeFav(Long ids) {

        return toAjax(proManagerService.updateStatusByManagerId(ids));
    }


}
